import 'dart:async';

import 'package:flutter/material.dart';
import 'package:ytbusiness/utils/asset_util.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/routes.dart';

import 'package:provider/provider.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/state/user_role.dart';
import 'package:ytbusiness/utils/sp_util.dart';

///广告页面
class AdPage extends StatefulWidget {
  @override
  _AdPageState createState() => _AdPageState();
}

class _AdPageState extends State<AdPage> {
  Timer _timer;
  int count = 3;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      startTime();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        alignment: Alignment(1.0, -1.0), // 右上角对齐
        children: [
          Container(
            width: setWidth(375),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  margin: EdgeInsets.only(top: setWidth(40), left: 15),
                  child: Image.asset(AssetUtil.icon("ic_ad_top"),
                      width: 43, height: 34),
                ),
                Container(
                  margin:
                      EdgeInsets.only(top: setWidth(20), left: setWidth(20)),
                  child: Text(
                    "超强阵容\n你的合作伙伴都在这里......",
                    style: TextStyle(
                        fontSize: setSp(16), color: Color(0xFF010101)),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    margin: EdgeInsets.only(
                        left: setWidth(15), right: setWidth(15)),
                    child: Image.asset(
                      AssetUtil.image("img_ad"), //广告图
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                )
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0.0, 30.0, 10.0, 0.0),
            child: FlatButton(
              color: Color.fromRGBO(0, 0, 0, 0.3),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                "$count 跳过",
                style: TextStyle(color: Colors.white, fontSize: 15.0),
              ),
              onPressed: () {
                navigationPage();
              },
            ),
          )
        ],
      ),
    );
  }

  void startTime() async {
    //设置启动图生效时间
    Timer(Duration(seconds: 1), () {
      // 空等1秒之后再计时
      _timer = Timer.periodic(const Duration(milliseconds: 1000), (v) {
        count--;
        if (count == 0) {
          navigationPage();
        } else {
          if (mounted) setState(() {});
        }
      });
      return _timer;
    });
  }

  void navigationPage() async {
    _timer.cancel();
    // await Navigator.of(context).pushReplacementNamed(RouteManager.scoutLogin);
    await Navigator.of(context).pushReplacementNamed(RouteManager.scoutHome);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _timer.cancel();
  }
}
